package com.jqb.springlab.processor;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.jqb.springlab.service.impl.PersonServiceImpl;
/**
 * 继承后置处理器(有点像Filter)
 * 实现对每个Bean的控制(AOP思想-面向多个对象)
 * @author Dong
 */
public class BeanProcessor implements BeanPostProcessor {

	//每个Bean实例化后就会马上来调用before
	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("BeanPostProcessor->before...bean create time:"+
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		if(arg0 instanceof PersonServiceImpl){
			System.out.println("->>PersonServiceImpl.class");
			PersonServiceImpl p = (PersonServiceImpl)arg0;
			p.setName("1111");
		}else{
			System.out.println("->>User.class");
		}
		return arg0;
	}
	//当Bean完全初始化后就会来调after(此Bean已可用)
	//(感觉就像在一个游戏里创建了一个人物[实例化]-练级[初始化]-满级[初始化完毕])
	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("BeanPostProcessor->after...");
		return arg0;
	}
}
